package com.example.xbrother.rule.aircondition;

import com.alibaba.fastjson.JSON;
import com.example.xbrother.rule.BaseBean;
import com.example.xbrother.rule.MaterialResult;
import com.example.xbrother.rule.excel.ExcelNoModelData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 空调内外机安装位置-bean
 */
@Slf4j
public class AirConditionPipeBean extends BaseBean {
    /**
     * 添加物料
     */
    public void add(AirConditionPipeParam p) {
        List<MaterialResult> result = p.getResult();
        Double yDistance = p.getVerticalDistance();
        Double xDistance = p.getDistanceBetweenOutAndInner() - yDistance;
        Double xValue = getXValue(xDistance);
        Double yValue = getYValue(yDistance);

        try {
            // todo: hcj 2023-02-06 db中查询出空调物料code对应的空调属性，用于判断是否是（列间 或者 底座空调）
            // A: 列间空调-铜管选型
            File excelFile = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "easy_rules/aircondition" + File.separator + "aircondition_pipe_column.xlsx");
            ExcelNoModelData excelNoModelData = readExcelData(excelFile, 1, 1);
            // 按不同的型号的“空调”遍历出铜管类型,eg:下面列举了2个不同型号的空调外机
            for (AirConditionPipeParam.AirConditionMaterial a : p.getAirConditionMaterialList()) {
                // 向上取整
                Double yDistanceT = Math.ceil(yDistance);
                // 向上取整
                Double xDistanceT = Math.ceil(xDistance);
                List<MaterialResult> resultTemp = handleMaterialResult(excelNoModelData, xValue, yValue, a.getAirConditionerCode(), a.getAirConditionerNum(), yDistanceT.intValue(), xDistanceT.intValue());
                result.addAll(resultTemp);
            }
            // B: 底座空调-铜管选型
            File excelFile_bed = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "easy_rules/aircondition" + File.separator + "aircondition_pipe_bed.xlsx");
            ExcelNoModelData excelNoModelDataBed = readExcelData(excelFile_bed, 1, 2);
            // result中的物料-铜管物料code一样的需要合并，统一输出一个总的物料
            BaseBean.mergeMaterialResult(p);
        } catch (Exception e) {
            log.error("物料处理失败，{}", e.getMessage());
        }
    }

    /**
     * 处理物料结果
     *
     * @param excelNoModelData excel数据
     * @param xValue           x轴的值
     * @param yValue           y轴的值
     * @param airConditionCode 空调编码
     * @param airConditionNum  空调数量（由空调选型结果中获取-注意是空调外机）
     * @param yDistance        垂直距离
     * @param xDistance        水平距离
     */
    private List<MaterialResult> handleMaterialResult(ExcelNoModelData excelNoModelData, Object xValue, Object yValue, String airConditionCode, Integer airConditionNum, Integer yDistance, Integer xDistance) {
        List<MaterialResult> result = new ArrayList<>();
        // 气管垂直-物料
        List<MaterialResult> materialList_gas_pipe_v = getMaterialFromExcelData(excelNoModelData, xValue, yValue, airConditionCode, "气管垂直");
        // 气管水平-物料
        List<MaterialResult> materialList_gas_pipe_l = getMaterialFromExcelData(excelNoModelData, xValue, yValue, airConditionCode, "气管水平");
        // 液管-物料
        List<MaterialResult> materialList_liquid_pipe = getMaterialFromExcelData(excelNoModelData, xValue, yValue, airConditionCode, "液管");
        // 气管-垂直-长度
        for (MaterialResult m : materialList_gas_pipe_v) {
            m.setNum(yDistance * airConditionNum);
            result.add(m);
        }
        // 气管-水平-长度
        for (MaterialResult m : materialList_gas_pipe_l) {
            m.setNum(xDistance * airConditionNum);
            result.add(m);
        }
        // 液管-长度
        for (MaterialResult m : materialList_liquid_pipe) {
            m.setNum((xDistance + yDistance) * airConditionNum);
            result.add(m);
        }
        return result;
    }

    /**
     * 从excel数据中获取具体的物料
     *
     * @param excelNoModelData excel数据
     * @param xValue           x轴的值
     * @param yValue           y轴的值
     * @param airConditionCode 空调编码
     * @param pipeType         铜管类型（气管垂直、气管水平、液管）
     */
    private List<MaterialResult> getMaterialFromExcelData(ExcelNoModelData excelNoModelData, Object xValue, Object yValue, String airConditionCode, String pipeType) {
        List<MaterialResult> result = new ArrayList<>();
        String materialJsonStr = positionMaterialInExcel(excelNoModelData, xValue, yValue, 2, t -> {
            // t代表的是excel中的行数据map, key为rowIndex, value为cellValue
            return t.get(0).equals(airConditionCode) && t.get(1).equals(pipeType);
        });
        result = JSON.parseArray(materialJsonStr, MaterialResult.class);
        log.info("result = {}", JSON.toJSONString(result));
        return result;
    }

    /**
     * 获取xValue
     */
    private double getXValue(double xDistance) {
        double xValue = 0d;
        if (xDistance >= 1d && xDistance <= 15d) {
            xValue = 10d;
        } else if (xDistance >= 16d && xDistance <= 25d) {
            xValue = 20d;
        } else if (xDistance >= 26d && xDistance <= 35d) {
            xValue = 30d;
        } else if (xDistance >= 36d && xDistance <= 45d) {
            xValue = 40d;
        } else if (xDistance >= 46d && xDistance <= 55d) {
            xValue = 50d;
        } else if (xDistance >= 56d && xDistance <= 60d) {
            xValue = 60d;
        }
        return xValue;
    }

    /**
     * 获取yValue
     */
    private double getYValue(double yDistance) {
        double yValue = 0d;
        if (yDistance >= 1d && yDistance <= 5d) {
            yValue = 5d;
        } else if (yDistance >= 6d && yDistance <= 10d) {
            yValue = 10d;
        } else if (yDistance >= 11d && yDistance <= 15d) {
            yValue = 15d;
        } else if (yDistance >= 16d && yDistance <= 20d) {
            yValue = 20d;
        }
        return yValue;
    }


}
